package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.ClassRemuneration;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.mapper.ClassRemunerationMapper;
import com.yuyou.fn.educational.service.IClassRemunerationService;
import com.yuyou.fn.educational.service.ITeacherService;
import com.yuyou.fn.educational.vo.ClassRemunerationQueryVo;
import com.yuyou.fn.educational.vo.ClassRemunerationSaveOrUpdateVo;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.platform.service.IBusTeamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 *
 * ClassRemuneration 表数据服务层接口实现类
 *
 */
@Service
public class ClassRemunerationServiceImpl extends SuperServiceImpl<ClassRemunerationMapper, ClassRemuneration,Long> implements IClassRemunerationService {


    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private IBusTeamService busTeamService;

    @Resource
    private ITeacherService teacherService ;

    @Override
    public Page<ClassRemuneration> findClassRemunerationForPage(ClassRemunerationQueryVo classRemunerationQueryVo, DataPermission dataPermission, int pageNo, int pageSize) {
        Example<ClassRemuneration> example = ClassRemuneration.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,classRemunerationQueryVo);

        if(dataPermission.getUserType() == 0 || dataPermission.getUserType() == 1 ){
            CriteriaUtils.fillConditionForAreaTeamPermission(criteria,dataPermission.getAreaTeamIds());
        }else{
            CriteriaUtils.fillConditionForBusPermission(criteria,dataPermission.getBusTeamIds());
        }

        return findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public void saveOrUpdateClassRemuneration(ClassRemunerationSaveOrUpdateVo classRemunerationSaveOrUpdateVo) {

        ClassRemuneration classRemuneration = new ClassRemuneration();

        BeanUtils.copyProperties(classRemuneration, classRemunerationSaveOrUpdateVo);
        if (classRemuneration.getAreaTeamId()==null || classRemuneration.getBusTeamId()==null || classRemuneration.getLevel()==null){
            throw new BusException("缺乏必填参数");
        }

        fillExtraData(classRemuneration);

        if (classRemuneration.getClassRemunerationId() == null) {
            ClassRemuneration check=this.findByBusTeamIdAndLevel(classRemuneration.getBusTeamId(),classRemuneration.getLevel());
            if (check!=null){
                throw new BusException("该业务组该课酬已设置，请前往编辑修改");
            }
            classRemuneration.setCreateTime(TimeUtils.now());
            classRemuneration.setUpdateTime(TimeUtils.now());
            insertSelective(classRemuneration);

        } else {
            classRemuneration.setUpdateTime(TimeUtils.now());
            updateSelectiveById(classRemuneration);
        }
    }

    private void fillExtraData(ClassRemuneration classRemuneration) {
        if (classRemuneration.getAreaTeamId() != null) {
            classRemuneration.setAreaTeamName(areaTeamService.findTeamNameById(classRemuneration.getAreaTeamId()));
        } else {
            classRemuneration.setAreaTeamName(StringUtils.EMPTY);
        }

        if (classRemuneration.getBusTeamId() != null) {
            classRemuneration.setBusTeamName(busTeamService.findTeamNameById(classRemuneration.getBusTeamId()));
        } else {
            classRemuneration.setBusTeamName(StringUtils.EMPTY);
        }
    }

    @Override
    public BigDecimal calRemuneration(Long areaTeamId, Long busTeamId, Long teacherId , int lectureAmount ) {

        Teacher teacher = teacherService.findById(teacherId);
        //首先查找业务组
        ClassRemuneration classRemuneration = findClassRemuneration(areaTeamId, busTeamId, teacher.getLevel());

        if(classRemuneration == null ){
            return BigDecimal.ZERO ;
        }
        return classRemuneration.getAmount().multiply(new BigDecimal( lectureAmount * 2 ));
    }

    private ClassRemuneration findClassRemuneration(Long areaTeamId, Long busTeamId, Integer level) {

        Example<ClassRemuneration> example = ClassRemuneration.createExample();
        example.orderBy("amount").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("busTeamId",busTeamId).andEqualTo("level" , level );
        ClassRemuneration classRemuneration = findOneByExample(example);
        if(classRemuneration != null ){
            return classRemuneration ;
        }

        example.clear();

        example.orderBy("amount").desc();
        criteria = example.createCriteria();
        criteria.andEqualTo("areaTeamId",areaTeamId).andEqualTo("level" , level );

        return findOneByExample(example);

    }

    @Override
    public ClassRemuneration findByBusTeamIdAndLevel(Long busTeamId, Integer level) {
        Example<ClassRemuneration> example=ClassRemuneration.createExample();
        example.createCriteria().andEqualTo("busTeamId",busTeamId).andEqualTo("level",level);
        return findOneByExample(example);
    }
}